package cn.ziyoufa.smart.smartschool.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import java.util.ArrayList;

import cn.ziyoufa.smart.smartschool.R;
import cn.ziyoufa.smart.smartschool.adapter.LogListAdapter;
import cn.ziyoufa.smart.smartschool.db.DBHelper;
import cn.ziyoufa.smart.smartschool.db.DBSqlHelper;
import cn.ziyoufa.smart.smartschool.module.BaseModel;
import cn.ziyoufa.smart.smartschool.module.shutdown.ShutdownValue;
import cn.ziyoufa.smart.smartschool.view.LoadMoreListView;

public class MainActivity extends AppCompatActivity {

    /**
     * UI
     */
    private LoadMoreListView mListView;
    private LogListAdapter adapter;
    private final String pageSize = "20";
    private int spage = 0;
    private ArrayList<BaseModel> mData = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navigateBackEvent();
        getDbData("0");
        initView();
    }

    private void navigateBackEvent(){
        final ImageView back = (ImageView) findViewById(R.id.home_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getDbData(String spage){
        String limit = spage+","+pageSize;
        if(DBHelper.getInstance().tabIsExist("ps_shutdown_log")) {
            //判断关机日志表是否存在
            //获取数据
            ArrayList<BaseModel> mShutdownValue = DBSqlHelper.getInstance().select(DBHelper.SHUT_TABLE
                    , null
                    , null
                    , "_id desc", ShutdownValue.class, limit);
            for(int i=0;i<mShutdownValue.size();i++){
                mData.add(mShutdownValue.get(i));
            }
        }
    }

    private void initView() {
        mListView = findViewById(R.id.log_list);
        adapter = new LogListAdapter(MainActivity.this, mData);
        mListView.setAdapter(adapter);
        mListView.setOnLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() {
            @Override
            public void onloadMore() {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        spage = spage+Integer.parseInt(pageSize);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                getDbData(String.valueOf(spage));
                                adapter.notifyDataSetChanged();
                                mListView.setLoadCompleted();
                            }
                        });
                    }
                }.start();
            }
        });
    }

}
